﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：UIPanel
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/7 9:56:56
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

#define UseLog

using System;
using System.Collections.Generic;
using TouchFish.PureMVC;
using UnityEngine;

namespace TouchFish.UI
{
    [Serializable]
    public class UIPanelType
    {
        /// <summary>
        /// UI Layer
        /// </summary>
        public UILayer Layer = UILayer.Common;
    }

    /// <summary>
    /// IUIPanel
    /// </summary>
    public interface IUIPanel : IViewComponent
    {
        Transform Transform { get; }
        UIPanelType UIPanelType { get; }

        IUIPanelInfo GetUIPanelInfo();

        void CreateUIPanel();

        void Show();

        void Hide();

        void DestoryUIPanel();
    }

    [Serializable]
    public class SubPanelInfo
    {
        public string PanelName;
        public UILayer Level;
    }

    public abstract class UIPanel : MonoBehaviour, IUIPanel
    {
        protected IFacade SelfFacade;

        private void Awake()
        {
            SelfFacade = Facade.GetInstance(FacadeKey.UIFacadeKey);
            InitSelfPanelInfoAndSelfPanelType();
        }

        protected virtual void OnEnable()
        {
            RegisterMediator();
        }

        protected virtual void OnDisable()
        {
            RemoveMediator();
        }

        protected abstract void RegisterMediator();

        protected abstract void RemoveMediator();

        protected abstract void InitSelfPanelInfoAndSelfPanelType();

        [SerializeField] private List<SubPanelInfo> mSubPanelInfos = new List<SubPanelInfo>();

        public Transform Transform
        {
            get { return transform; }
        }

        [SerializeField]
        protected UIPanelType SelfUIPanelType = new UIPanelType();

        public UIPanelType UIPanelType { get { return SelfUIPanelType; } }

        public IViewMediator SelfMediator { get; set; }

        #region 面板加载

        /// <summary>
        /// 面板加载器
        /// </summary>
        private IPanelLoader _panelLoader = null;

        /// <summary>
        /// 面板加载
        /// </summary>
        /// <param name="panelPath">面板路径</param>
        /// <param name="assetBundleName">ab 路径</param>
        /// <returns></returns>
        public static UIPanel Load(string panelPath, string assetBundleName = null)
        {
            var panelLoader = new DefaultPanelLoader();
            var panelPrefab = string.IsNullOrEmpty(assetBundleName)
                ? panelLoader.LoadPanelPrefab(panelPath)
                : panelLoader.LoadPanelPrefab(assetBundleName, panelPath);
            var obj = Instantiate(panelPrefab);
            var retScript = obj.GetComponent<UIPanel>();
            retScript._panelLoader = panelLoader;
            return retScript;
        }

        #endregion 面板加载

        /// <summary>
        /// 面板创建成功，调用一次， UIManager 自动调用
        /// </summary>
        /// <param name="uiPanelData"></param>
        public void CreateUIPanel()
        {
            OnCreate();
        }

        /// <summary>
        /// 对象创建时调用 初始化UI  用于 1 代码动态获取 UI对象 2 对象的初始化
        /// </summary>
        protected virtual void OnCreate()
        {
            LogMessage(GetType().Name + "  OnCreate");
        }

        protected void HideSelf()
        {
            UIMgr.HidePanel(SelfUIPanelInfo.UIResId);
        }

        protected void CloseSelf()
        {
            UIMgr.ClosePanel(SelfUIPanelInfo.UIResId);
        }

        /// <summary>
        /// 对象关闭(销毁)时调用 ,不允许子类调用
        /// </summary>
        public void DestoryUIPanel()
        {
            OnDestory();

            Destroy(gameObject);

            _panelLoader.Unload();
            _panelLoader = null;

            //mSubPanelInfos.ForEach(subPanelInfo => UIMgr.ClosePanel(subPanelInfo.PanelName));
            //mSubPanelInfos.Clear();
        }

        protected virtual void OnDestory()
        {
            LogMessage(GetType().Name + "  OnDestory");
            SelfUIPanelInfo.UIPanelState = UIPanelState.Destory;
        }

        public virtual void Show()
        {
            LogMessage(GetType().Name + "  Show");
            SelfUIPanelInfo.UIPanelState = UIPanelState.Show;
            gameObject.SetActive(true);
        }

        public virtual void Hide()
        {
            LogMessage(GetType().Name + "  Hide");
            SelfUIPanelInfo.UIPanelState = UIPanelState.Hide;
            gameObject.SetActive(false);
        }

        protected UIPanelInfo SelfUIPanelInfo = new UIPanelInfo();

        public IUIPanelInfo GetUIPanelInfo()
        {
            return SelfUIPanelInfo;
        }

        public void LogMessage(string message)
        {
#if UseLog
            Debug.Log(message);
#endif
        }

        public void Initialize()
        {
        }
    }
}